競プロ典型 90 問 014
種別: 記事
カテゴリ: 競技プログラミング
サブカテゴリ: AtCoder > 競プロ典型 90 問
タグ: #解いた問題
(工事中)
AtCoder で公開されている競プロ典型 90 問 の014 を解いた
解き方
解答例
下は上記の方法で解いたときの提出結果である。また、その提出の際に提出したソースコードをその下に転記する。
code: C
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
int main () {
int n = 0;
int a100000 = {};
int b100000 = {};
int res = 0;
long long ans = 0;
res = scanf("%d", &n);
for (int i = 0; i < n; i++) {
res = scanf("%d", a+i);
}
for (int i = 0; i < n; i++) {
res = scanf("%d", b+i);
}
qsort(a, n, sizeof(int), cmp);
qsort(b, n, sizeof(int), cmp);
for (int i = 0; i < n; i++) {
ans += (long long) abs(ai - bi);
}
printf("%lld\n", ans);
return 0;
}
私の提出一覧
table: submissions_atcoder_typical90_014
提出のURL 提出時刻 結果 備考
1回目 https://atcoder.jp/contests/typical90/submissions/22281377 2021-05-03T18:24:22+0900 AC
感想
ローカルにおいてあったzakkan.txt(雑感)に書かれていたこと
014 - We Used to Sing a Song Together(★3)
日時: 2021-05-03 18:24:22
提出: https://atcoder.jp/contests/typical90/submissions/22281377
生徒と学校を1対1で対応させるので、
"順番に"対応させていくのが最適と推測される。
実際に、異なる対応の場合を考えるてみれば、
順番が入れ替わっているところを元に戻しても
不満度は増えないことがわかる。
それがわかれば後はソートして前から対応させていくだけ